package cn.gok;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

public class 遍历 {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        System.out.println(map.put(1, "aaa"));
        System.out.println(map.put(2, "bbb"));
        System.out.println(map.put(1, "ccc"));
        for (Integer key : map.keySet()) {
            String value = map.get(key);
            System.out.println(key + "---" + value);
        }
        Iterator<Integer> it = map.keySet().iterator();
        while (it.hasNext()) {
            Integer key = it.next();
            String value = map.get(key);
            System.out.println(key + "---" + value);
        }
        for (Map.Entry<Integer, String> e : map.entrySet()) {
            Integer key = e.getKey();
            String value = e.getValue();
            System.out.println(key + "---" + value);
        }
        Consumer<Integer> consumer = (k) -> {
            System.out.println(k + "*****" + map.get(k));
        };
        map.keySet().stream().forEach(consumer);

        Iterator<Map.Entry<Integer, String>> it2 = map.entrySet().iterator();

        while (it2.hasNext()) {
            Map.Entry<Integer, String> e = it2.next();
            System.out.println(e.getKey() + "---" + e.getValue());
        }
        Consumer<Map.Entry<Integer, String>> consumer2 = (e) -> {

            System.out.println(e.getKey() + "****" + e.getValue());
        };
        map.entrySet().stream().forEach(consumer2);
        Runnable r2 = () -> {
            System.out.println("!!!");
        };
        r2.run();

        doRun(() -> {
            System.out.println("ss");
        });
        Function接口();
        简化lambda();
        流构造();

    }

    private static void 流构造() {
        Stream s1 = Stream.of("a","b","c");
        s1.forEach(System.out::println);
        System.out.println("--------");
        List<Integer> list = Arrays.asList(100,21,10,16);
        Stream s2 = list.stream();
        s2.forEach(System.out::println);
        System.out.println("--------");

    }

    private static void 简化lambda() {
        Consumer<String> consumer = System.out::println;
        Function<String, Integer> function = Integer::parseInt;
        String str = "abcdefg";
        Function<Integer, String> function2 = str::substring;
        System.out.println(function2.apply(2));

    }

    private static void doRun(Runnable r) {
        r.run();
    }

    public static void Function接口() {
        Function<String, Integer> function1 = str -> str.length();
        System.out.println(function1.apply("我是xx"));
        Function<Integer, Integer> function2 = i -> i + 100;
        System.out.println(function2.compose(function1).apply("我是xxx"));
        Function<Float, Float> function3 = i -> i * i;
        Function<Float, Float> function4 = i -> i * 100;
        System.out.println(function3.apply(2.0f));

    }

}



